package com.linkage.jz.bankruptcy.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.bean.MyProInfo;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.androidcommon.adapter.BGARecyclerViewAdapter;
import cn.bingoogolapple.androidcommon.adapter.BGAViewHolderHelper;
import cn.bingoogolapple.swipeitemlayout.BGASwipeItemLayout;


public class PublicProListViewAdapter extends BGARecyclerViewAdapter<MyProInfo> {
    /**
     * 当前处于打开状态的item
     */
    private List<BGASwipeItemLayout> mOpenedSil = new ArrayList<>();
    private Context mcontext;
    public PublicProListViewAdapter(RecyclerView recyclerView, Context context) {

        super(recyclerView, R.layout.item_my_pro);
        mcontext = context;
//        super(recyclerView, R.layout.item_bgaswipe);
    }

    @Override
    public void setItemChildListener(BGAViewHolderHelper viewHolderHelper, int viewType) {
        BGASwipeItemLayout swipeItemLayout = viewHolderHelper.getView(R.id.sil_item_bgaswipe_root);
        swipeItemLayout.setDelegate(new BGASwipeItemLayout.BGASwipeItemLayoutDelegate() {
            @Override
            public void onBGASwipeItemLayoutOpened(BGASwipeItemLayout swipeItemLayout) {
                closeOpenedSwipeItemLayoutWithAnim();
                mOpenedSil.add(swipeItemLayout);
            }

            @Override
            public void onBGASwipeItemLayoutClosed(BGASwipeItemLayout swipeItemLayout) {
                mOpenedSil.remove(swipeItemLayout);
            }

            @Override
            public void onBGASwipeItemLayoutStartOpen(BGASwipeItemLayout swipeItemLayout) {
                closeOpenedSwipeItemLayoutWithAnim();
            }
        });
        viewHolderHelper.setItemChildClickListener(R.id.tv_item_bgaswipe_delete);
        viewHolderHelper.setItemChildLongClickListener(R.id.tv_item_bgaswipe_delete);
        viewHolderHelper.setItemChildClickListener(R.id.et_item_bgaswipe_read);
        viewHolderHelper.setItemChildLongClickListener(R.id.et_item_bgaswipe_read);
    }

    @Override
    public void fillData(BGAViewHolderHelper viewHolderHelper, int position, MyProInfo model) {
         String strStatus = "";
        if("0".equalsIgnoreCase(model.getProjectStatus()))
        {
            strStatus = "暂存";
            viewHolderHelper.setTextColor(R.id.tv_item_status, R.color.black);

//            viewHolderHelper.setText(R.id.bt_item_bt1, "申请加入")
//              .setText(R.id.bt_item_bt2,"删除");

            viewHolderHelper.setVisibility(R.id.bt_item_bt1, View.GONE)
            .setVisibility(R.id.bt_item_bt2, View.GONE);
            if (model.getProjectColumnList().getAllowJoin())
            {
                viewHolderHelper.setText(R.id.bt_item_bt1, "申请加入");
                viewHolderHelper.setVisibility(R.id.bt_item_bt1, View.VISIBLE)
                        .setVisibility(R.id.bt_item_bt2, View.GONE);
                viewHolderHelper.setItemChildClickListener(R.id.bt_item_bt1);
            }
        }else if("1".equalsIgnoreCase(model.getProjectStatus()))
        {
            strStatus = "已关闭";
            viewHolderHelper.setTextColor(R.id.tv_item_status, R.color.darker_gray);
//            viewHolderHelper.setVisibility(R.id.bt_item_bt1, View.GONE)
//                    .setVisibility(R.id.bt_item_bt2, View.GONE);


            if("true".equalsIgnoreCase(model.getAllowJoin()))
            {
                viewHolderHelper.setText(R.id.bt_item_bt1, "申请加入");
                viewHolderHelper.setBackgroundRes(R.id.bt_item_bt1,R.drawable.pro_o_corner_btn);
                viewHolderHelper.setVisibility(R.id.bt_item_bt1, View.VISIBLE)
                 .setVisibility(R.id.bt_item_bt2, View.GONE);
                viewHolderHelper.setItemChildClickListener(R.id.bt_item_bt1);
            }
        }else if("2".equalsIgnoreCase(model.getProjectStatus()))
        {
            viewHolderHelper.setTextColor(R.id.tv_item_status, R.color.page_top);
            strStatus = "程序中";
            viewHolderHelper.setVisibility(R.id.bt_item_bt1, View.GONE)
                    .setVisibility(R.id.bt_item_bt2, View.GONE);
            viewHolderHelper.setVisibility(R.id.bt_item_bt1, View.GONE)
                    .setVisibility(R.id.bt_item_bt2, View.GONE);
            if("true".equalsIgnoreCase(model.getAllowJoin()))
            {
                viewHolderHelper.setText(R.id.bt_item_bt1, "申请加入");
                viewHolderHelper.setBackgroundRes(R.id.bt_item_bt1,R.drawable.pro_o_corner_btn);
                viewHolderHelper.setVisibility(R.id.bt_item_bt1, View.VISIBLE)
                        .setVisibility(R.id.bt_item_bt2, View.GONE);
                viewHolderHelper.setItemChildClickListener(R.id.bt_item_bt1);
            }
//            viewHolderHelper.setText(R.id.bt_item_bt1, "申请加入");
//            viewHolderHelper.setVisibility(R.id.bt_item_bt2, View.GONE);
        }else if("3".equalsIgnoreCase(model.getProjectStatus()))
        {
            strStatus = "已结案";
            viewHolderHelper.setTextColor(R.id.tv_item_status, R.color.darker_gray);
            viewHolderHelper.setVisibility(R.id.bt_item_bt1, View.GONE)
                    .setVisibility(R.id.bt_item_bt2, View.GONE);
//            viewHolderHelper.setVisibility(R.id.bt_item_bt1, View.GONE)
//                    .setVisibility(R.id.bt_item_bt2, View.GONE);
        }

        String logo = model.getProjectLogo();

        String proName = model.getProjectName();

        if("".equalsIgnoreCase(logo))
        {
            if(proName.length()>2) {
                viewHolderHelper.setText(R.id.pro_title, proName.substring(0,2));
            }
        }else {
            viewHolderHelper.setText(R.id.pro_title, "");
            ImageView img = (ImageView)viewHolderHelper.getImageView(R.id.tv_item_bgaswipe_img);
            Glide.with(mcontext).load(model.getProjectLogo())
                    .placeholder(R.drawable.nopic)
                    .error(R.drawable.nopic).into(img);
        }

        viewHolderHelper
                .setText(R.id.tv_item_status, strStatus)
                .setText(R.id.tv_item_bgaswipe_detail, model.getProjectName())
               ;



        BGASwipeItemLayout swipeItemLayout = viewHolderHelper.getView(R.id.sil_item_bgaswipe_root);
//        if (position % 3 == 0) {
            swipeItemLayout.setSwipeAble(false);
//        } else {
//            swipeItemLayout.setSwipeAble(true);
//        }
    }

    public void closeOpenedSwipeItemLayoutWithAnim() {
        for (BGASwipeItemLayout sil : mOpenedSil) {
            sil.closeWithAnim();
        }
        mOpenedSil.clear();
    }
}